package com.aaa.dao.impl;

import com.aaa.dao.RoleDao;
import com.aaa.entity.Role;
import com.aaa.entity.TreeMenu;
import com.aaa.util.BaseDao;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RoleDaoImpl implements RoleDao {

    private BaseDao baseDao = BaseDao.getInstance();

    @Override
    public List<Map<String, Object>> getAllRole() {
        String sql = "select * from role";
        return baseDao.query(sql,null);
    }

    @Override
    public List<Map<String, Object>> findRole(Integer pageNumber, Integer pageSize,String roleName) {

        String sql = "select * from role where 1 = 1 ";
        if (StringUtils.isNotBlank(roleName)) {
            roleName = "%" + roleName + "%";
            sql += "and roleName like '" + roleName + "'";
        }
        sql += " limit ?,?";
        Object[] params = {pageNumber-1, pageSize};
        return baseDao.query(sql,params);
    }

    @Override
    public int findRoleCount(String roleName) {
        String sql = "select count(1) as len from role where 1 = 1 ";
        if (StringUtils.isNotBlank(roleName)) {
            roleName = "%" + roleName + "%";
            sql += "and roleName like '" + roleName + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

    @Override
    public int updateRoleById(Role role) {
        String sql = "update role set roleName = ?,description = ?,status = ? where id = ?";
        Object [] params = {role.getRoleName(),role.getDescription(),role.getStatus(),role.getId()};
        return baseDao.executeUpdate(sql,params);
    }

    @Override
    public int addRole(Role role) {
        String sql = "insert into role(roleName,description,status) values(?,?,?)";
        Object [] params = {role.getRoleName(),role.getDescription(),role.getStatus()};
        return baseDao.executeUpdate(sql,params);
    }

    @Override
    public int deleteStaffByRoleId(int roleId) {
        String sql = "delete from role where id = ?";
        Object[] params = {roleId};
        return baseDao.executeUpdate(sql,params);
    }

    @Override
    public List<TreeMenu> getMenuList(int roleId) {

        // 1.找到角色 已经拥有的权限

        Set<Integer> ids = getRsourceByRoleId(roleId);

        // 2.查到所有父类的列表
        String sql = "select rs.id nodeid,rs.resource_name text,rs.icon icon,rs.pid pid,rs.sort sort " +
                "               from resource rs " +
                "                where rs.pid = 0 and rs.status = 1 order by rs.id asc;";


        List<TreeMenu> listParent =   baseDao.query(sql,null,TreeMenu.class);

        // 3.遍历每一个父列表
        for (TreeMenu treeMenu:listParent ){
            // 标记父类是否勾选
            if (ids.contains(treeMenu.getNodeid())){
                treeMenu.setState("selected");
            }
            //4.查到每一父亲的孩子列表
            String sql1 = "select rs.id nodeid,rs.resource_name text,rs.icon icon,rs.pid pid,rs.sort sort " +
                    "               from resource rs " +
                    "                where rs.pid = ? and rs.status = 1 order by rs.id asc;";

            Object[] objects = {treeMenu.getNodeid()};
            List<TreeMenu> listNodes =   baseDao.query(sql1,objects,TreeMenu.class);
            // 标记子类是否勾选
            for (TreeMenu node:listNodes){
                if (ids.contains(node.getNodeid())){
                    node.setState("selected");
                }
            }
            // 设置子节点
            treeMenu.setTreeMenuList(listNodes);

        }

        return listParent;
    }

    public Set<Integer> getRsourceByRoleId(Integer roleId){
        String sqlRole = "SELECT resource_id rId from resource_role where role_id =  ?";

        Object[] paramRole = {roleId};

        List<Map<String,Object>> idList = baseDao.query(sqlRole,paramRole);

        Set<Integer> ids = new HashSet<>();

        for (Map<String,Object> map:idList){
            long id  = (long) map.get("rId");
            ids.add((int)id);
        }

        return ids;
    }



}
